<?php
require_once 'core/config.php';
require_once 'core/string_helper.php';
require_once 'core/url_helper.php';
require_once 'core/loader.php';

if (empty($_GET['type']) || empty($_GET['content'])) {
    echo "<script>alert('No argument passed!');history.back();</script>";
    exit();
}
$search_type = $_GET['type'];
$search_content = $_GET['content'];
$model = load_model('crash_model');
$crashes = array();

// switch($search_type) {
// case "backtrace":
// case "crash_report":
// $crashes = $model->search_like($search_type, $search_content);
// break;
// default:
// $crashes = $model->search('reporter', $search_content);
// echo "Search Type error!";
// }
$crashes = $model->search_like($search_type, $search_content);
$upload_time = isset($_GET['time'])?$_GET['time']:NULL;
if (count($crashes) > 0) {
    foreach ($crashes as &$crash) {
        $backtrace = $crash['backtrace'];
        $array_backtrace = explode("\n", $backtrace);
        $crash['crash_title'] = "";
        $crash['highlight'] = FALSE;
        if ($upload_time == $crash['upload_time']) {
            $crash['highlight'] = TRUE;
        }
        // get libsys.so first crash line
        if ($crash['process_type'] == PROCESS_TYPE_NATIVE) {
            $is_hydra = FALSE;
            foreach ($array_backtrace as $line) {
                if (strstr($line, "libsys.so")) {
                    $crash['crash_title'] = $line;
                    $is_hydra = TRUE;
                    break;
                }
            }
            if ($is_hydra == FALSE) {
                $crash['crash_title'] = $crash['process'];
            }
        } else if ($crash['process_type'] == PROCESS_TYPE_JAVA) {
            foreach ($array_backtrace as $line) {
                if (strstr($line, "Exception") || strstr($line, "Error")) {
                    $crash['crash_title'] = $line;
                    break;
                }
            }
        }
    }
}
load_view('main_view', array(
    'crash_reports'=>$crashes,
    'action'=>'search',
    'search_type'=>$search_type,
    'search_content'=>$search_content
));
